package day27_35;

/**
 * @ClassName Solution
 * @Description 一次遍历，遇到target值就返回对应的索引，遇到第一个大于target的值，也返回当前索引；利用一个Boolean变量判断是否需要在数组尾部添加target元素，
 * @Author clockTown
 * @Date 2021/6/5 16:48
 * @Version 1.0
 */
public class Solution {
    public static int searchInsert(int[] nums, int target) {
        boolean isAdd = false;
        for (int i = 0; i < nums.length; i++) {
            if (target == nums[i]){
                isAdd = true;
                return i;
            }
            if (target < nums[i]){
                isAdd = true;
                return i - 1;
            }
        }

        if (!isAdd){
            return nums.length;
        }

        return -1;
    }

    public static void main(String[] args) {
        int[] nums = {1,3,5,6};
        int target = 2;
        System.out.println(searchInsert(nums, target));
    }
}
